package com.dm0858.bianmin.mine.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

import com.dm0858.bianmin.R;
import com.dm0858.bianmin.databinding.ActivityBindPhoneLayoutBinding;
import com.dm0858.bianmin.mine.base.BaseActivity;
import com.dm0858.bianmin.mine.listener.IResultListener;
import com.dm0858.bianmin.mine.listener.IWXWithPhoneListener;
import com.dm0858.bianmin.mine.tools.SPUtils;
import com.dm0858.bianmin.mine.tools.ShapeUtils;
import com.dm0858.bianmin.mine.tools.ToastUtils;
import com.dm0858.bianmin.mine.viewmodel.LoginViewModel;
import com.dm0858.bianmin.mine.viewmodel.MineViewModel;

/**
 * @author 迪迪丶Sir
 * @description 绑定手机号
 * @e-mail asd969709719@qq.com
 * @since 2020-05-17
 **/
public class BindPhoneActivity extends BaseActivity implements View.OnClickListener, Spinner.OnItemSelectedListener {

    private ActivityBindPhoneLayoutBinding binding;

    private MineViewModel viewModel;

    private String openid = "";

    //spinner的适配器
    private ArrayAdapter arrayAdapter;
    //区号
    private String[] areaCodes = new String[]{
            "+86", "+87", "+88", "+89", "+90"
    };
    private String areaCode = areaCodes[0];

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(this,R.layout.activity_bind_phone_layout);
        binding.setOnClick(this);

        //设置按钮的样式
        binding.btnGetCode.setBackground(
                new ShapeUtils()
                        .corner(2)
                        .lineColor(getResources().getColor(R.color.appTheme,null))
                        .lineWidth(1)
                        .build()
        );
        binding.btnCommit.setBackground(
                new ShapeUtils()
                        .corner(2)
                        .fill(getColor(R.color.appTheme))
                        .build()
        );
        initData();
    }

    @Override
    protected void initData() {
        arrayAdapter = new ArrayAdapter(this, R.layout.spinner_area_code_item_layout, R.id.txt_areaCode, areaCodes);
        binding.spinner.setAdapter(arrayAdapter);
        viewModel = new ViewModelProvider(this).get(MineViewModel.class);
        openid = getIntent().getExtras().getString("openid","");
        getTime();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                onBackPressed();
                break;
            case R.id.btn_getCode:
                if (binding.edtPhone.getText().toString().equals("")) {
                    ToastUtils.showShort("手机号不能为空");
                    return;
                }
                viewModel.getCode(binding.edtPhone.getText().toString(), object -> setTime(60));
                break;
            case R.id.btn_commit:
                if (binding.edtPhone.getText().toString().equals("")) {
                    ToastUtils.showShort("手机号不能为空");
                    return;
                }
                if (binding.edtCode.getText().toString().equals("")) {
                    ToastUtils.showShort("请获取验证码");
                    return;
                }
                viewModel.wxWithPhone(openid, binding.edtPhone.getText().toString(), binding.edtCode.getText().toString(), () -> onBackPressed());
                break;
        }
    }



    @Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
        areaCode = areaCodes[i];
    }

    @Override
    public void onNothingSelected(AdapterView<?> adapterView) {

    }


    /**
     * @description 设置时间开始倒计时
     * @param
     * @return
     * @author 迪迪丶Sir
     * @since 2020-04-09
     **/
    private CountDownTimer timer;
    private void setTime(int time) {
        //将倒计时结束时的时间保存
        SPUtils.getInstance("user_info").put("code_remainingTime",System.currentTimeMillis() + time * 1000,true);
        if (time > 0) {
            binding.btnGetCode.setTextColor(getResources().getColor(R.color.grey_700,null));
            if(binding.btnGetCode.isClickable())binding.btnGetCode.setClickable(false);
//            if (binding.txtCountdown.getVisibility() == View.INVISIBLE) binding.txtCountdown.setVisibility(View.VISIBLE);
            timer = new CountDownTimer(time * 1000,1000) {
                @Override
                public void onTick(long l) {
                    int seconds = (int)(l / 1000);
                    binding.btnGetCode.setText(seconds + "s");
                }

                @Override
                public void onFinish() {
                    binding.btnGetCode.setTextColor(getResources().getColor(R.color.appTheme,null));
                    binding.btnGetCode.setText("验证码");
                    if(!binding.btnGetCode.isClickable())binding.btnGetCode.setClickable(true);
                }
            };
            timer.start();
        }
        else {
            binding.btnGetCode.setTextColor(getResources().getColor(R.color.appTheme,null));
            if(!binding.btnGetCode.isClickable())binding.btnGetCode.setClickable(true);
        }
    }

    /**
     * @description 获取剩余倒计时
     * @param
     * @return
     * @author 迪迪丶Sir
     * @since 2020-04-09
     **/
    private void getTime() {
        //当前时间
        long currentTime = System.currentTimeMillis();
        //倒计时结束时的时间
        long countdown = SPUtils.getInstance("user_info").getLong("code_remainingTime",currentTime);
        //剩余时间
        int remainingTime = ((int)(currentTime - countdown)) / 1000;
        //如果剩余时间时负数，说明倒计时还没完
        if (remainingTime < 0) remainingTime = Math.abs(remainingTime);
        else remainingTime = 0;
        setTime(remainingTime);
    }
}